package org.come.control;

import java.util.List;

import org.come.Frame.ZhuFrame;
import org.come.action.FromServerAction;
import org.come.lianhua.AutoMaticRefiningJframe;
import org.come.until.FormsManagement;
import org.come.until.GsonUtil;
import org.come.until.Music;
import org.come.until.Util;

import com.tool.image.ImageMixDeal;
import com.tool.time.TimeLimit;
import come.tool.Fighting.FightingManData;
import come.tool.Fighting.FightingMixDeal;
import come.tool.Fighting.Statelist;
import come.tool.FightingData.BattleConnection;
import come.tool.handle.HandleState;

public class BattleConnectionControl implements FromServerAction{
	@Override
	public void controlMessFromServer(String mes,String type) {
		// TODO Auto-generated method stub
		FightingMixDeal.State=HandleState.USUAL;
		BattleConnection battleConnection=GsonUtil.getGsonUtil().getgson().fromJson(mes, BattleConnection.class);
		if (battleConnection.getFightingNumber()==-1) {
			FightingMixDeal.FightingEnd();
			return;
		}
		FightingMixDeal.BattleType=battleConnection.getBattleType();
		FightingMixDeal.FightingNumber=battleConnection.getFightingNumber();
		FightingMixDeal.CurrentRound=battleConnection.getRound();
		FightingMixDeal.BattlefieldPlay.clear();
		Statelist statelist=new Statelist();
		statelist.setRound(battleConnection.getPlayeEvents());
		FightingMixDeal.BattlefieldPlay.clear();
		FightingMixDeal.BattlefieldPlay.put(battleConnection.getRound(),statelist);
		FightingMixDeal.CorrectTime=0;
		FightingMixDeal.systime=System.currentTimeMillis();
		FightingMixDeal.CurrentData(battleConnection.getDatas(),battleConnection.getBuff(),getcamp(battleConnection.getDatas()));
		FightingMixDeal.CurrentRound=battleConnection.getRound();	
		if (battleConnection.getState()==0) {
			FightingMixDeal.State=HandleState.HANDLE_WAIT;
			FightingMixDeal.time=battleConnection.getTime();
		}else if (battleConnection.getState()==1) {
			FightingMixDeal.State=battleConnection.getEventState();
			FightingMixDeal.time=battleConnection.getTime();
			if (FightingMixDeal.State==0) {
                FightingMixDeal.State=HandleState.HANDLE_WAIT;
            }
		}else if (battleConnection.getState()==2) {
			FightingMixDeal.State=HandleState.HANDLE_PLAY;
			//处理播放进度
			long pass=battleConnection.getTime();
			while (pass>0) {
				FightingMixDeal.PalyProgress(36);
				pass-=36;
			}
		}else if (battleConnection.getState()==3) {
			FightingMixDeal.State=HandleState.HANDLE_END;
		}
		AutoMaticRefiningJframe.getAssistantJframe().getAssistantJpanel().setCount(0);
		AutoMaticRefiningJframe.getAssistantJframe().getAssistantJpanel().getStart().setText("运行");
		FormsManagement.HideForm(999);
		FormsManagement.HideForm(74);
		FormsManagement.HideForm(11);
		FormsManagement.HideForm(120);
		ZhuFrame.getZhuJpanel().add(ZhuFrame.getZhuJpanel().getZidong());
		ZhuFrame.getZhuJpanel().add(ZhuFrame.getZhuJpanel().getZidong111());
		ZhuFrame.getZhuJpanel().HideBeastBtn();
		if (FightingMixDeal.State==HandleState.HANDLE_PLAYER) {
			ZhuFrame.getZhuJpanel().ShowManBtn(FightingMixDeal.isLL());
		}else if (FightingMixDeal.State==HandleState.HANDLE_PET) {
			ZhuFrame.getZhuJpanel().ShowBeastBtn();
		}
		Music.addbeijing("w"+(Util.random.nextInt(12)+1)+".mp3");
		if (FightingMixDeal.BattleType==10&&FightingMixDeal.CurrentRound==1&&FightingMixDeal.camp==1) {
			TimeLimit.getLimits().removeLimit(TimeLimit.getLimits().getLimit("杀人香"));
		}
	}
	/**
     * 获取阵营
     */
	public FightingManData getcamp(List<FightingManData> fightingManDatas){
		String rolename=ImageMixDeal.userimg.getRoleShow().getRolename();
		for (int i = 0; i < fightingManDatas.size(); i++) {
			FightingManData data=fightingManDatas.get(i);
			if (data.getType()!=0) {
                continue;
            }
			if (data.getManname().equals(rolename)) {
                return data;
            }
		}
		return null;		
	}
}
